import sys
parent_dir = "../pygl"
sys.path.append(parent_dir)
import ctypes
import OpenGL.GL as gl
import glm
import imgui
import glfw
from glfw import _GLFWwindow as GLFWwindow
import numpy as np
import pygl
from common import AppWithTrackball, AxesHelper
from parametric_surface import BreatherSurface, AppleSurface, KleinSurface

class SolidWireframeApp(AppWithTrackball):
    # 初始化着色器，vao vbo等
    def setupVertex(self) -> None:
        gl.glEnable(gl.GL_DEPTH_TEST)
        self.program = pygl.ProgramVFG("usecase/shaders/solid_wireframe.vs", 
                                      "usecase/shaders/solid_wireframe.fs",
                                      "usecase/shaders/solid_wireframe.geom")
        self.background_color = glm.vec4(1.0, 1.0, 1.0, 1.0)
        self.apple_surface = AppleSurface()
        self.klein_surface = KleinSurface()
        self.breather_surface = BreatherSurface(60, 100)

        self.current_surface = self.apple_surface
        self.world_axes = AxesHelper(10.0, 5.0)
        self.show_world_axes = True
        self.imgui_window_width = 250
        self.imgui_window_height = 240
        self.surface_color = glm.vec3(146.0/255.0, 150.0/255.0, 242.0/255.0)
        self.line_color = glm.vec3(0.3, 0.3, 0.3)
        self.camera.zoom_speed = 2.0
        self.camera.reset(glm.vec3(20.0, 10.0, 30.0), glm.vec3(0.0, 0.0, 0.0), 
                    glm.vec3(0.0, 1.0, 0.0))

    # 配置imgui界面元素
    def showImGui(self)->None:
        imgui.begin("设置")
        if imgui.button("重置"):
            self.camera.reset(glm.vec3(20.0, 10.0, 30.0), glm.vec3(0.0, 0.0, 0.0), 
                    glm.vec3(0.0, 1.0, 0.0))
        if imgui.radio_button("苹果曲面", self.current_surface == self.apple_surface):
            self.current_surface = self.apple_surface
        if imgui.radio_button("克莱因曲面", self.current_surface == self.klein_surface):
            self.current_surface = self.klein_surface
        if imgui.radio_button("Breather曲面", self.current_surface == self.breather_surface):
            self.current_surface = self.breather_surface

        _, color = imgui.color_edit3("曲面颜色", self.surface_color.x, 
                    self.surface_color.y, self.surface_color.z)
        self.surface_color = glm.vec3(color[0], color[1], color[2])
        _, color = imgui.color_edit3("线框颜色", self.line_color.x, 
                    self.line_color.y, self.line_color.z)
        self.line_color = glm.vec3(color[0], color[1], color[2])
        _, self.show_world_axes = imgui.checkbox("显示三维世界坐标轴", 
                                                self.show_world_axes)
        imgui.end()       

    # 渲染图形
    def render(self) -> None:
        gl.glClearBufferfv(gl.GL_COLOR, 0, self.background_color.to_tuple())
        gl.glClearBufferfv(gl.GL_DEPTH, 0, 1.0)
        self.view = self.camera.getViewMatrix()
        self.projection = self.camera.getProjectionMatrix()
        self.program.use()
        self.current_surface.updateModelMatrix()
        self.program.setUniformMatrix4fv(0, self.current_surface.model_matrix)
        self.program.setUniformMatrix4fv(1, self.view)
        self.program.setUniformMatrix4fv(2, self.projection)
        self.program.setUniform1f(3, 1.0)
        self.program.setUniform3fv(4, self.surface_color)
        self.program.setUniform3fv(5, self.line_color)
        self.current_surface.render()
        gl.glDisable(gl.GL_POLYGON_OFFSET_LINE)
        if self.show_world_axes:
            self.world_axes.updateModelMatrix
            self.world_axes.render(self.view, self.projection)

    # 释放资源
    def cleanup(self) -> None:
        self.apple_surface.delete()
        self.klein_surface.delete()
        self.breather_surface.delete()
        self.world_axes.delete()
        self.program.delete()

    def mouse_move(self, window:GLFWwindow, x:float, y:float)->None:
        if imgui.is_any_item_active():
            return
        super().mouse_move(window, x, y)

if __name__ == "__main__":
    app = SolidWireframeApp(100, 100, 1000, 600, "你好，几何着色器App！")
    app.run()


